<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Traits\Curl;

class IndexController extends Controller
{
    use Curl;

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    public function index(Request $request)
    {
        $key = $request->get('key', '');
        $response = ['books' => []];
        if (!empty($key)) {
            $url = "http://se.qidian.com/";
            $res = self::curlGet($url, ['kw' => $key]);
            preg_match_all("/<h4><a href=\"(.*?)\" .+?>(.+?)<\/a><\/h4>/", $res, $result);
            $response['books'] = $result[2];
            $response['urls'] = $result[1];
        }
        return view('index', $response);
    }
}
